﻿using Contracts;
using Prism.Mvvm;
using Prism.Navigation.Regions;
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Threading;

namespace Shell.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly INavigationRegistry _navigationRegistry;
        private readonly IModuleEventAggregator _moduleEventAggregator;
        private INavigationItem _selectedNavigationItem;
        private string _currentTime;

        public ObservableCollection<INavigationItem> NavigationItems { get; } = new ObservableCollection<INavigationItem>();

        public INavigationItem SelectedNavigationItem
        {
            get => _selectedNavigationItem;
            set
            {
                SetProperty(ref _selectedNavigationItem, value);
                if (value != null)
                {
                    NavigateToModule(value.NavigationPath);
                }
            }
        }

        public string CurrentTime
        {
            get => _currentTime;
            set => SetProperty(ref _currentTime, value);
        }

        public MainWindowViewModel(
            IRegionManager regionManager,
            INavigationRegistry navigationRegistry,
            IModuleEventAggregator moduleEventAggregator)
        {
            _regionManager = regionManager;
            _navigationRegistry = navigationRegistry;
            _moduleEventAggregator = moduleEventAggregator;

            // 初始化时钟
            var timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += (s, e) => CurrentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            timer.Start();

            // 订阅导航项变化
            _navigationRegistry.CollectionChanged += OnNavigationItemsChanged;

            // 订阅模块加载完成事件
            _moduleEventAggregator.SubscribeModulesLoaded(OnModulesLoaded);
        }

        private void OnModulesLoaded()
        {
            // 模块加载完成后初始化导航
            InitializeNavigation();
        }

        private void OnNavigationItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // 当导航项变化时更新UI
            if (e.NewItems != null)
            {
                foreach (INavigationItem item in e.NewItems)
                {
                    if (!NavigationItems.Contains(item))
                    {
                        NavigationItems.Add(item);
                    }
                }
            }

            // 设置初始选中的模块
            if (NavigationItems.Count > 0 && SelectedNavigationItem == null)
            {
                SelectedNavigationItem = NavigationItems.First();
            }
        }

        private void InitializeNavigation()
        {
            // 清除现有项
            NavigationItems.Clear();

            // 添加所有导航项
            foreach (var item in _navigationRegistry.NavigationItems)
            {
                NavigationItems.Add(item);
            }

            // 设置初始选中的模块
            if (NavigationItems.Count > 0)
            {
                SelectedNavigationItem = NavigationItems.First();
            }
        }

        private void NavigateToModule(string navigationPath)
        {
            if (!string.IsNullOrEmpty(navigationPath))
            {
                _regionManager.RequestNavigate("MainContentRegion", navigationPath);
            }
        }
    }
}